package cn.weakup.designpattern.b_creational.factory.ifactory;

/**
 * @author lixj
 * @version 1.0.0
 * @date 2022/6/2
 * @desc
 */
public abstract class Operation {
    private Double numberA;
    private Double numberB;

    public Double getNumberA() {
        return numberA;
    }

    public void setNumberA(Double numberA) {
        this.numberA = numberA;
    }

    public Double getNumberB() {
        return numberB;
    }

    public void setNumberB(Double numberB) {
        this.numberB = numberB;
    }

    /**
     * 由子类实现具体的运算逻辑
     * @return
     */
    abstract Double getResult();
    /**
     * 处理计算逻辑
     * @param numberA
     * @param numberB
     * @param option
     * @return
     */
    public static String handleProgram(String numberA, String numberB, String option) {
        Double result;
        Operation operation;
        IOperationFactory operationFactory;
        try {
            // 工厂方法模式将判断逻辑移动到客户端判断
            switch (option) {
                case "+":
                    operationFactory = new AddFactory();
                    break;
                case "-":
                    operationFactory = new SubFactory();
                    break;
                case "*":
                    operationFactory = new MulFactory();
                    break;
                case "/":
                    operationFactory = new DivFactory();
                    break;
                default:
                    throw new IllegalStateException("Unexpected value: " + option);
            }
            operation = operationFactory.createOperate(option);
            operation.setNumberA(Double.valueOf(numberA));
            operation.setNumberB(Double.valueOf(numberB));
            result = operation.getResult();
        } catch (Exception e) {
            return e.getMessage();
        }
        return result.toString();
    }
}
